//
//  Fish_EditInfoViewController.swift
//  WoodenFishApp
//
//  Created by mac on 2023/10/18.
//  Copyright © 2023 Fish_. All rights reserved.
//

import UIKit

class Fish_EditInfoViewController: BasicViewController {
    
    let avarB = UIButton.initV(img: "Avatar")
    let titleL = UILabel.initV(.setTypeFace(20.Fit,type: .medium), text: "Edit Avatar", textColor: mainColor.withAlphaComponent(0.3))
    let txtfiled1 = UITextField.init(frame: .zero)
    var imgUrl = ""
    private lazy var doneButton: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = .setTypeFace(18.Fit,type: .bold)
        button.setTitleColor(.black, for: .normal)
        button.setTitle("Save", for: .normal)
        button.backgroundColor = mainColor
        button.layer.cornerRadius = 25
        return button
    }()
    private lazy var descView: Custom_TextView = {
        let view = Custom_TextView()
        view.backgroundColor = UIColor.white
        view.textContainerInset = UIEdgeInsets(top: 10.Fit, left: 10.Fit, bottom: 10.Fit, right: 10.Fit)
        view.tintColor = mainColor
        view.textColor = mainColor
        view.placeHolderColor = UIColor.color(hexString: "#FFFFFF",alpha: 0.3)
        view.placeHolder = "Set a signature"
        view.returnKeyType = .done
        view.font = .setTypeFace(16.Fit,type: .medium)
        return view
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.imgUrl = USEINFOMODEL.headImage ?? ""
        if let url = URL(string: USEINFOMODEL.headImage ?? "") {
            self.avarB.kf.setImage(with: url, for: .normal, placeholder: .init(named: "avarB"))
        }
        self.txtfiled1.text  = USEINFOMODEL.name
        if USEINFOMODEL.userDesc?.count != 0{
            self.descView.text = USEINFOMODEL.userDesc
        }
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            avarB
            titleL
            txtfiled1
            descView
            doneButton
            
        }
        title = "Personal Information"
        
        avarB.backgroundColor = .gray
        avarB.size(150.Fit).top(30.Fit + NavHeight)
        avarB.centerHorizontally()
        avarB.setAllCorners(corner: 35.Fit)
        avarB.addAct {
            let page = Edit_Choose_Photo_Interface(type: .photoPicker)
            page.chooseImageCompletion = { [weak self] image in
               Toast_View_Tool.show()
               RT.uploadCoverImage(file: (self?.imageToBase64String(image: image))!) { [self] (dict) in
                    Toast_View_Tool.dismiss()
                   self?.avarB.setImage(image, for: .normal)
                   self?.imgUrl = dict.url!
               }
            }
            self.present(page, animated: true)
        }

        titleL.left(30.Fit).right(30.Fit).height(40)
        titleL.Top == avarB.Bottom + 15.Fit
        titleL.textAlignment = .center
        
        txtfiled1.left(30.Fit).right(30.Fit).height(70.Fit)
        let attriText = NSMutableAttributedString.init(string: "Nick name")
        attriText.addAttributes([NSAttributedString.Key.foregroundColor: mainColor], range: NSRange(location: 0, length: attriText.string.count))
        txtfiled1.attributedPlaceholder = attriText
        txtfiled1.textColor = mainColor
        txtfiled1.backgroundColor = .init(white: 1, alpha: 0.15)
        txtfiled1.font = .setTypeFace(16.Fit,type: .bold)
        txtfiled1.Top == titleL.Bottom + 50.Fit
        txtfiled1.setAllCorners(corner: 20.Fit)
        
        let v1 = UIView.init(frame: .init(x: 0, y: 0, width: 60, height: 50))
        let img1 = UIImageView.initImgView(img: "NameIcon")
        img1.frame = .init(x: 15, y: 5, width: 40, height: 40)
        v1.addSubview(img1)
        txtfiled1.leftView = v1
        txtfiled1.leftViewMode = .always

        descView.left(30.Fit).right(30.Fit).height(150.Fit)
        descView.backgroundColor = .init(white: 1, alpha: 0.15)
        descView.Top == txtfiled1.Bottom + 15.Fit
        descView.setAllCorners(corner: 20.Fit)
        
        doneButton.left(30.Fit).right(30.Fit).height(50).bottom(15.Fit + TabbarSafeBottomMargin)
        doneButton.addAct {
            guard self.txtfiled1.text?.count != 0 else {
                Toast_View_Tool.show(type: .error,text: "The name cannot be empty.")
                return
            }
            guard self.descView.text?.count != 0 else {
                Toast_View_Tool.show(type: .error,text: "The signature cannot be empty.")
                return
            }

            guard self.imgUrl.count != 0 else {
                Toast_View_Tool.show(type: .error,text: "Please upload your avatar.")
                return
            }
            let para : [String:Any] = ["headImage":self.imgUrl,
                                       "name":self.txtfiled1.text ?? "",
                                       "userId":USEINFOMODEL.id ?? "","userDesc":self.descView.text ?? ""]
            Toast_View_Tool.show()
            RT.EditUserInfoWith(paraInfo: para) { info in
                RT.getUserInfoUrlInfoWith { info in
                    Toast_View_Tool.dismiss()
                    self.navigationController?.popViewController(animated: true)
                }
            }
        }
        
        
        
    }
        
}



